/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package ms.jasim.model.event.impl;

import ms.jasim.framework.IEventSimulationInfoProvider;
import ms.jasim.framework.IJasimAppContext;
import ms.jasim.framework.EventSimulator.EventHistory;
import ms.jasim.framework.SolutionSimulator.SimulatorArg;
import ms.jasim.model.ModelPackage;
import ms.jasim.model.event.Event;
import ms.jasim.model.event.EventPackage;
import ms.jasim.model.event.EventRelativeTime;
import ms.jasim.model.impl.ModelObjectImpl;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Event Relative Time</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link ms.jasim.model.event.impl.EventRelativeTimeImpl#getTimeOffset <em>Time Offset</em>}</li>
 *   <li>{@link ms.jasim.model.event.impl.EventRelativeTimeImpl#getEvent <em>Event</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public class EventRelativeTimeImpl extends ModelObjectImpl implements EventRelativeTime {
	/**
	 * The default value of the '{@link #getTimeOffset() <em>Time Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTimeOffset()
	 * @generated
	 * @ordered
	 */
	protected static final int TIME_OFFSET_EDEFAULT = 0;

	/**
	 * The cached value of the '{@link #getTimeOffset() <em>Time Offset</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getTimeOffset()
	 * @generated
	 * @ordered
	 */
	protected int timeOffset = TIME_OFFSET_EDEFAULT;

	/**
	 * The cached value of the '{@link #getEvent() <em>Event</em>}' reference.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getEvent()
	 * @generated
	 * @ordered
	 */
	protected Event event;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected EventRelativeTimeImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return EventPackage.Literals.EVENT_RELATIVE_TIME;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public int getTimeOffset() {
		return timeOffset;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setTimeOffset(int newTimeOffset) {
		int oldTimeOffset = timeOffset;
		timeOffset = newTimeOffset;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EventPackage.EVENT_RELATIVE_TIME__TIME_OFFSET, oldTimeOffset, timeOffset));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Event getEvent() {
		if (event != null && event.eIsProxy()) {
			InternalEObject oldEvent = (InternalEObject)event;
			event = (Event)eResolveProxy(oldEvent);
			if (event != oldEvent) {
				if (eNotificationRequired())
					eNotify(new ENotificationImpl(this, Notification.RESOLVE, EventPackage.EVENT_RELATIVE_TIME__EVENT, oldEvent, event));
			}
		}
		return event;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public Event basicGetEvent() {
		return event;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public void setEvent(Event newEvent) {
		Event oldEvent = event;
		event = newEvent;
		if (eNotificationRequired())
			eNotify(new ENotificationImpl(this, Notification.SET, EventPackage.EVENT_RELATIVE_TIME__EVENT, oldEvent, event));
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case EventPackage.EVENT_RELATIVE_TIME__TIME_OFFSET:
				return getTimeOffset();
			case EventPackage.EVENT_RELATIVE_TIME__EVENT:
				if (resolve) return getEvent();
				return basicGetEvent();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case EventPackage.EVENT_RELATIVE_TIME__TIME_OFFSET:
				setTimeOffset((Integer)newValue);
				return;
			case EventPackage.EVENT_RELATIVE_TIME__EVENT:
				setEvent((Event)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case EventPackage.EVENT_RELATIVE_TIME__TIME_OFFSET:
				setTimeOffset(TIME_OFFSET_EDEFAULT);
				return;
			case EventPackage.EVENT_RELATIVE_TIME__EVENT:
				setEvent((Event)null);
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case EventPackage.EVENT_RELATIVE_TIME__TIME_OFFSET:
				return timeOffset != TIME_OFFSET_EDEFAULT;
			case EventPackage.EVENT_RELATIVE_TIME__EVENT:
				return event != null;
		}
		return super.eIsSet(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public String toString() {
		if (eIsProxy()) return super.toString();

		StringBuffer result = new StringBuffer(super.toString());
		result.append(" (TimeOffset: ");
		result.append(timeOffset);
		result.append(')');
		return result.toString();
	}

	
	@Override
	public boolean conditionMatch(IJasimAppContext context, SimulatorArg arg) {
		IEventSimulationInfoProvider info = context.getService(IEventSimulationInfoProvider.class);
		if (info != null) {
			EventHistory lastEvent = info.getEventHistory().get(this.getEvent().getName());
			if (lastEvent != null && lastEvent.getTime() + this.getTimeOffset() == arg.getTime())
				return true;
		}
		return false;
	}

	
	@Override
	public Object getValue(String name) {
		return null;
	}

	
	@Override
	public boolean hasValue(String name) {
		return false;
	}

	
	@Override
	public void setValue(String name, Object value) {
		
	}

} //EventRelativeTimeImpl
